package com.lishe.lishe.lishe.widget;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fynn.fluidlayout.FluidLayout;
import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.mall.bean.SkuObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shino on 2017/9/20.
 * 地址选择的popupwindow
 */

public class SelectAddressPopupwindow extends PopupWindow {

    public SelectAddressPopupwindow(Context context) {
        super(context);
    }

    public static class Builder {
        private Context context;
        private List<String> addDresss = new ArrayList<>();
        private List<SkuObject> addSkus = new ArrayList<>();
        private List<LinearLayout> skuTextViwes = new ArrayList<>();
        private List<TextView> skuTextViwes0 = new ArrayList<>();
        private List<LinearLayout> addressTextViwes = new ArrayList<>();
        private List<TextView> addressTextViwes0 = new ArrayList<>();

        private int goods_count = 1;
        private String name;
        private String imgUrl;
        private int discount_intagral;
        private int intagral;
        private ImageLoaderUtils imageLoaderUtils;

        private int checkAddressIndex = 0;//地址栏选中标志
        private int checkSkuIndex = 0;//sku栏选中标志
        private SelectAddressPopupwindow popupwindow;
        private int defaultColor;
        private int selectColor;

        public Builder(Context context, int defaultColor, int selectColor) {
            this.context = context;
            imageLoaderUtils = new ImageLoaderUtils();
            this.defaultColor = defaultColor;
            this.selectColor = selectColor;
        }

        //添加地址项
        public Builder addItem(String addDress) {
            addDresss.add(addDress);
            return this;
        }

        //添加地址项
        public Builder addItem(List<String> addDresss) {
            this.addDresss.clear();
            this.addDresss.addAll(addDresss);
            return this;
        }

        //添加sku
        public Builder setSku(SkuObject sku) {
            addSkus.add(sku);
            return this;
        }

        //添加sku
        public Builder setSku(List<SkuObject> addSkus) {
            this.addSkus.clear();
            this.addSkus.addAll(addSkus);
            return this;
        }

        //商品名
        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        //商品数量
        public Builder setCount(int count) {
            goods_count = count;
            return this;
        }

        //折后积分
        public Builder setDiscountIntegral(int discount_intagral) {
            this.discount_intagral = discount_intagral;
            return this;
        }

        //折前积分
        public Builder setIntegral(int intagral) {
            this.intagral = intagral;
            return this;
        }

        //折前积分
        public Builder setImgUrl(String imgUrl) {
            this.imgUrl = imgUrl;
            return this;
        }

        /**
         * 创建popupwindow
         */
        public SelectAddressPopupwindow create() {
            popupwindow = new SelectAddressPopupwindow(context);
            popupwindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            popupwindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//            ColorDrawable drawable=new ColorDrawable();
//            drawable.setColor(Color.parseColor("#200D0D0D"));
            popupwindow.setBackgroundDrawable(new BitmapDrawable());//去掉间隔
            View view = initView(popupwindow);
            popupwindow.setContentView(view);
            return popupwindow;
        }

        /**
         * 初始化视图
         *
         * @return
         */

        private View initView(final SelectAddressPopupwindow popupwindow) {
            addressTextViwes.clear();
            skuTextViwes.clear();
            addressTextViwes0.clear();
            skuTextViwes0.clear();
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.item_popupwidow_add_goods, null);
            //---------------------列表栏----------------------
            LinearLayout addressLayout = view.findViewById(R.id.address_layout);
            LinearLayout default_shape_layout = view.findViewById(R.id.shape_layout);
            final TextView defalut_format = view.findViewById(R.id.defalut_format);
            FluidLayout skuLayout = view.findViewById(R.id.sku_layout);
            LinearLayout add_address_shape_layout = view.findViewById(R.id.shape_add_address);
            final TextView add_address = view.findViewById(R.id.addAddress);
            //---------------------图片及常量----------------------
            ImageView close = view.findViewById(R.id.close);
            ImageView goods_img = view.findViewById(R.id.goods_image);
            TextView texName = view.findViewById(R.id.name);
            TextView texDiscountIntegral = view.findViewById(R.id.discount_integral);
            TextView texIntegral = view.findViewById(R.id.integral);
            texIntegral.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            TextView redus = view.findViewById(R.id.redus);
            final TextView count = view.findViewById(R.id.count);
            TextView augment = view.findViewById(R.id.augment);
            Button sure = view.findViewById(R.id.sure);
            //------------------------初始化数据------------------------
            count.setText("" + goods_count);
            if (addSkus == null || addSkus.size() == 0) {
                default_shape_layout.setVisibility(View.VISIBLE);
            } else {
                default_shape_layout.setVisibility(View.GONE);
            }
            if (imgUrl != null) {
                imageLoaderUtils.displayImage(imgUrl, goods_img);
            }
            if (name != null) {
                texName.setText(name);
            }
            texDiscountIntegral.setText(discount_intagral + "");
            if (intagral > 0) {
                texIntegral.setVisibility(View.VISIBLE);
                texIntegral.setText(intagral + "积分");
            } else {
                texIntegral.setVisibility(View.GONE);
            }

            //------------------------监听------------------------
            close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupwindow.dismiss();
                }
            });
            //减
            redus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (goods_count > 1) {
                        goods_count -= 1;
                        count.setText(goods_count + "");
                    }
                    if (itemOnclickListener != null) {
                        if (itemOnclickListener != null) {
                            itemOnclickListener.onAdd_RedusOnclickListener(-1, goods_count, count);
                        }
                    }
                }
            });
            //加
            augment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    goods_count += 1;
                    count.setText(goods_count + "");
                    if (itemOnclickListener != null) {
                        itemOnclickListener.onAdd_RedusOnclickListener(1, goods_count, count);
                    }
                }
            });
            //确定
            sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemOnclickListener != null) {
                        itemOnclickListener.onComfitOnclickListener(goods_count);
                    }
                }
            });
            //默认规格
            defalut_format.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    skuTextViwes.get(checkSkuIndex).setBackgroundResource(R.drawable.shape_coner5px);
                    skuTextViwes0.get(checkSkuIndex).setTextColor(defaultColor);
                    checkSkuIndex = 0;
                    skuTextViwes.get(checkSkuIndex).setBackgroundResource(R.drawable.shape_red5px);
                    skuTextViwes0.get(checkSkuIndex).setTextColor(selectColor);
                    if (itemOnclickListener != null) {
                        itemOnclickListener.onSkuItemOnclickListener(true, defalut_format.getText().toString());
                    }
                }
            });
            //添加地址
            add_address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addressTextViwes.get(checkAddressIndex).setBackgroundResource(R.drawable.shape_coner5px);
                    addressTextViwes0.get(checkAddressIndex).setTextColor(defaultColor);
                    checkAddressIndex = 0;
                    if (itemOnclickListener != null) {
                        itemOnclickListener.onAddressItemOnclickListener(true, add_address.getText().toString(), -1);
                    }
                }
            });
            //-----------------------动态生成view-------------------------
            addressLayout.removeAllViews();
            skuLayout.removeAllViews();
            addressTextViwes.add(add_address_shape_layout);
            skuTextViwes.add(default_shape_layout);
            addressTextViwes0.add(add_address);
            skuTextViwes0.add(defalut_format);
            addAddresView(inflater, addressLayout);
            addSkuView(inflater, skuLayout);
            return view;
        }

        /**
         * 动态生成地址View
         *
         * @param inflater
         * @param addressLayout
         */
        private void addAddresView(LayoutInflater inflater, LinearLayout addressLayout) {
            for (int i = 0; i < addDresss.size(); i++) {
                View shape_textviwe = inflater.inflate(R.layout.item_shape_textviwe5px, null);
                LinearLayout shape_layout = shape_textviwe.findViewById(R.id.shape_layout);
                final TextView tv = shape_textviwe.findViewById(R.id.defalut_format);
                tv.setText(addDresss.get(i));
                tv.setTextSize(14);
                tv.setTag(i + 1);
                final int finalI = i;
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        addressTextViwes.get(checkAddressIndex).setBackgroundResource(R.drawable.shape_coner5px);
                        addressTextViwes0.get(checkAddressIndex).setTextColor(defaultColor);
                        checkAddressIndex = ((Integer) tv.getTag());
                        addressTextViwes.get(checkAddressIndex).setBackgroundResource(R.drawable.shape_red5px);
                        addressTextViwes0.get(checkAddressIndex).setTextColor(selectColor);
                        if (itemOnclickListener != null) {
                            itemOnclickListener.onAddressItemOnclickListener(false, tv.getText().toString(), finalI);
                        }
                    }
                });
                addressTextViwes.add(shape_layout);
                addressTextViwes0.add(tv);
                addressLayout.addView(shape_textviwe);
            }
        }

        /**
         * 动态生成skuView
         *
         * @param inflater
         * @param skuLayout
         */
        private void addSkuView(LayoutInflater inflater, FluidLayout skuLayout) {
            for (int i = 0; i < addSkus.size(); i++) {
                if (i != 0 && TextUtils.isEmpty(addSkus.get(i).getSpec_info())) {
                    continue;
                }
                View shape_textviwe = inflater.inflate(R.layout.item_shape_textviwe5px, null);
                LinearLayout shape_layout = shape_textviwe.findViewById(R.id.shape_layout);
                final TextView tv = shape_textviwe.findViewById(R.id.defalut_format);
                if (TextUtils.isEmpty(addSkus.get(i).getSpec_info())) {
                    tv.setText("默认规格");
                } else {
                    tv.setText(addSkus.get(i).getSpec_info());
                }
                tv.setTextSize(14);
                tv.setTag(i + 1);
                tv.setGravity(Gravity.CENTER);
                final int finalI = i;
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        skuTextViwes.get(checkSkuIndex).setBackgroundResource(R.drawable.shape_coner5px);
                        skuTextViwes0.get(checkSkuIndex).setTextColor(defaultColor);
                        checkSkuIndex = ((Integer) tv.getTag());
                        skuTextViwes.get(checkSkuIndex).setBackgroundResource(R.drawable.shape_red5px);
                        skuTextViwes0.get(checkSkuIndex).setTextColor(selectColor);
                        if (itemOnclickListener != null) {
                            itemOnclickListener.onSkuItemOnclickListener(false, addSkus.get(finalI).getSku_id());
                        }
                    }
                });
                skuTextViwes.add(shape_layout);
                skuTextViwes0.add(tv);
                skuLayout.addView(shape_textviwe);
            }
        }


        //关掉
        public void dissmiss() {
            popupwindow.dismiss();
        }

        //相对某个控件的位置（正左下方），无偏移
        public void showAsDropDown(View anchor) {
            create().showAsDropDown(anchor);
        }

        //相对某个控件的位置，有偏移;xoff表示x轴的偏移，正值表示向左，负值表示向右；yoff表示相对y轴的偏移，正值是向下，负值是向上；
        public void showAsDropDown(View anchor, int xoff, int yoff) {
            create().showAsDropDown(anchor, xoff, yoff);
        }

        //相对于父控件的位置（例如正中央Gravity.CENTER，下方Gravity.BOTTOM等），可以设置偏移或无偏移
        public void showAtLocation(View parent, int gravity, int x, int y) {
            create().showAtLocation(parent, gravity, x, y);
        }

        /**
         * 回调事件
         */
        ItemOnclickListener itemOnclickListener;

        public interface ItemOnclickListener {
            void onAddressItemOnclickListener(boolean isAdd, String address, int position);//地址点击监听

            void onSkuItemOnclickListener(boolean isDefalut, String skuId);//sku点击监听

            void onAdd_RedusOnclickListener(int index, int count, TextView view);//加减点击监听

            void onComfitOnclickListener(int count);//确认按钮点击监听

        }

        public void setOnItemOnclickListener(ItemOnclickListener itemOnclickListener) {
            this.itemOnclickListener = itemOnclickListener;
        }
    }
}
